package org.jsola.hr.web;

import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.PersonalEventAddDTO;
import org.jsola.hr.dto.PersonalEventUpdateDTO;
import org.jsola.hr.query.PersonalEventQuery;
import org.jsola.hr.service.IPersonalEventService;
import org.jsola.hr.vo.PersonalEventListVO;
import org.jsola.hr.vo.PersonalEventResultByYearVO;
import org.jsola.hr.vo.PersonalEventVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


/**
 * 人事变动表
 *
 * @author ds
 */
@RestController("hrPersonalEventController")
@RequestMapping(HrConstants.API_PATH + "/v1/personalEvent")
public class PersonalEventController implements IPersonalEventControllerAPI {

    @Autowired
    private IPersonalEventService personalEventService;

    @Override
    @PostMapping
    public Result<PersonalEventVO> save(@Valid @RequestBody PersonalEventAddDTO personalEventAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增人事变动表权限校验

        return Result.success(
                personalEventService.save(personalEventAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody PersonalEventUpdateDTO personalEventUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改人事变动表权限校验
        if (personalEventUpdateDTO.getIfAuto()) {
            throw new ParamException("更新失败，系统自动录入不支持修改");
        }
        int count = personalEventService.update(personalEventUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }


    @Override
    @DeleteMapping(value = "/{personalEventId}")
    public Result<Integer> delete(@PathVariable Long personalEventId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除人事变动表权限校验

        int count = personalEventService.deleteById(tokenUser, personalEventId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody Long[] personalEventIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除人事变动表权限校验

        int count = personalEventService.deleteByIds(tokenUser, personalEventIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{personalEventId}")
    public Result<PersonalEventVO> findById(@PathVariable Long personalEventId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看人事变动表权限校验

        return Result.success(
                personalEventService.selectById(personalEventId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<PersonalEventListVO>> listAll(@Valid PersonalEventQuery personalEventQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看人事变动表权限校验

        return Result.success(
                personalEventService.select(personalEventQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping(value = "/selectByEventTime")
    public Result<List<PersonalEventResultByYearVO>> selectByEmpId(@Valid String empId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看人事变动表权限校验

        return Result.success(
                personalEventService.selectByEmpId(empId, tokenUser)
        );
    }

//    @Override
//    @GetMapping(value = "/selectByUserId")
//    public Result<List<PersonalEventResultByYearVO>> selectByUserId(@Valid String userId) {
//        TokenUser tokenUser = UserContext.getUser();
//        // todo 查看人事变动表权限校验
//
//        return Result.success(
//                personalEventService.selectByUserId(userId, tokenUser)
//        );
//    }


    @Override
    @GetMapping
    public Result<Page<PersonalEventListVO>> page(@Valid PersonalEventQuery personalEventQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                personalEventService.selectPage(personalEventQuery, tokenUser.getSiteId())
        );
    }


}

